package com.java.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.java.pojo.Items;
import com.java.pojo.ItemsImg;
import com.java.pojo.ItemsParam;
import com.java.pojo.ItemsSpec;
import com.java.pojo.vo.CommentLevelCountsVO;
import com.java.pojo.vo.ItemCommentVO;
import com.java.pojo.vo.ItemInfoVO;
import com.java.pojo.vo.ShopcartVO;
import com.java.service.ItemsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.java.common.utils.PageUtils;
import com.java.common.utils.R;



/**
 * 商品表 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表
 *
 * @author jl
 * @since 2020-10-13 11:31:41
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/items")
public class ItemsController {
    @Autowired
    private ItemsService itemsService;

    @ApiOperation("根据规格ids查询最新的购物车中的数据")
    @GetMapping("/refresh")
    public R refresh(@RequestParam String itemSpecIds) {
        List<ShopcartVO> list = itemsService.queryItemsBySpecIds(itemSpecIds);
        return R.ok(list);
    }

    @ApiOperation("通过分类ID搜索商品列表")
    @GetMapping("/catItems")
    public R search(@RequestParam(value = "catId") Integer catId, @RequestParam(value = "sort",required = false) String sort,
                    @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,@RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {
        PageUtils pages = itemsService.searchItems(catId, sort, page, pageSize);
        return R.ok(pages);
    }

    @ApiOperation("搜索商品列表")
    @GetMapping("/search")
    public R search(@RequestParam(value = "keywords",required = false) String keywords, @RequestParam(value = "sort",required = false) String sort,
                     @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,@RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {
        PageUtils pages = itemsService.searchItems(keywords, sort, page, pageSize);
        return R.ok(pages);
    }

    @ApiOperation("查询商品评价")
    @GetMapping("/comments")
    public R comment(@RequestParam(value = "itemId") String itemId, @RequestParam(value = "level",required = false) Integer level,
                     @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,@RequestParam(value = "pageSize",required = false,defaultValue = "10") Integer pageSize) {
        PageUtils pages = itemsService.queryPageItemComments(page, pageSize, level, itemId);
        return R.ok(pages);
    }

    /**
     * 根据商品ID查询商品评价数量
     */
    @GetMapping("/commentLevel")
    public R commentLevel(@RequestParam String itemId) {
        CommentLevelCountsVO commentLevelCountsVO = itemsService.queryCommentCounts(itemId);
        return R.ok(commentLevelCountsVO);
    }

    /**
     * 带条件分页查询商品表 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = itemsService.queryPage(params);

	    return R.ok(page);
    }

    /**
     * 根据主键查询商品表 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表
     */
    @GetMapping("/info/{itemId}")
    public R info(@PathVariable("itemId") String itemId){
		Items items = itemsService.queryItemById(itemId);
        List<ItemsImg> itemsImgs = itemsService.queryItemImgList(itemId);
        List<ItemsSpec> itemsSpecs = itemsService.queryItemSpecList(itemId);
        ItemsParam itemsParam = itemsService.queryItemParam(itemId);
        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgs);
        itemInfoVO.setItemSpecList(itemsSpecs);
        itemInfoVO.setItemParams(itemsParam);
        return R.ok(itemInfoVO);
    }

    /**
     * 新增商品表 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表
     */
    @PostMapping("/save")
    public R save(@RequestBody Items items){
		itemsService.save(items);

        return R.ok();
    }

    /**
     * 修改商品表 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表
     */
    @PutMapping("/update")
    public R update(@RequestBody Items items){
		itemsService.updateById(items);

        return R.ok();
    }

    /**
     * 删除商品表 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody String[] ids){
		itemsService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
